<?php
header("Content-Type: text/html");
header("charset: utf-8");
include_once("_common.php");

$wr = sql_fetch(" select * from $bo_table where wr_id = '$wr_id' ");

$returnVal = "0";
if ($wr['wr_id']) {

$conStr = " select max(wr_comment) as max_comment from $bo_table
			where wr_parent = '$wr_id' and wr_is_comment = 1 ";
$row = sql_fetch($conStr);
$row[max_comment] += 1;
$tmp_comment = $row[max_comment];
$tmp_comment_reply = "";

//순서를 가장 밑으로 정하기
if ($orderby != 0){
$ordStr = " select max(orderby)+1 as max_ordeby from $bo_table
			where wr_parent = '$wr_id' and wr_is_comment = 1 and orderby < $orderby and step=1";
$row = sql_fetch($ordStr);
if ($row[max_ordeby])
	$orderby = $row[max_ordeby];
else
	$orderby = 0;
}

// 계층형댓글 순서처리
sql_query(" update $bo_table set orderby = orderby + 1 where wr_parent = '$wr_id' and wr_is_comment = 1 and orderby >= '$orderby' ");

$conStr = " insert into $bo_table
                set ca_name = '$wr[ca_name]',
                    wr_num = '$wr[wr_num]',
                    wr_parent = '$wr_id',
                    wr_is_comment = '1',
                    wr_comment = '$tmp_comment',
                    wr_comment_reply = '$tmp_comment_reply',
                    wr_content = '".urldecode($re_content)."',
                    mb_id = '$mb_id',
                    wr_password = (select mb_password from g4_member where mb_id = '$mb_id'),
                    wr_name = (select mb_nick from g4_member where mb_id = '$mb_id'),
                    wr_email = (select mb_email from g4_member where mb_id = '$mb_id'),
                    wr_datetime = now(),
                    wr_last = '',
                    wr_ip = '$_SERVER[REMOTE_ADDR]',
                    orderby = '$orderby',
                    step = '$step',
                    family = '$family'
                    ";
sql_query($conStr);

// 원글에 코멘트수 증가 & 마지막 시간 반영
sql_query(" update $bo_table set wr_comment = wr_comment + 1, wr_last = now() where wr_id = '$wr_id' ");

$wr = sql_query(" select *, (select case when (a.mb_icon is null or a.mb_icon = '') and mb_sex='F' then '/img/product/icon_people1.gif' when (a.mb_icon is null or a.mb_icon = '') then '/img/product/icon_people.gif' else a.mb_icon end from g4_member a where a.mb_id = b.mb_id) as mb_icon, (select count(*) from $bo_table a where b.wr_id = a.family) as family_cnt from $bo_table b where wr_parent = '$wr_id' and wr_is_comment = 1 order by orderby desc ");

$returnVal = "<table>";
for ($i=0; $row=sql_fetch_array($wr); $i++){
	$returnVal .= "\n	<tr>";
	$returnVal .= "\n		<td>";
	if(intval($row['step']) > 1){
		//for($k=1; $k<intval($row['step']); $k++){
			$returnVal .= "\n	&nbsp;&nbsp;&nbsp;&nbsp;";
		//}
		$returnVal .= "\n		<img src=\"../img/product/icon_re.gif\" width=\"17\" height=\"11\" hspace=\"3\" vspace=\"3\" align=\"absmiddle\" />";
	}
	
	$returnVal .= "\n			".$row['wr_name']."<img src=\"{$row['mb_icon']}\" width=\"22\" height=\"23\" align=\"absmiddle\" />&nbsp;&nbsp;&nbsp;&nbsp;".$row['wr_content'];
	if($member['mb_id']){
	$returnVal .= "\n			&nbsp;&nbsp;<img src=\"../img/product/bt_re.gif\" style=\"cursor:pointer;\" onclick=\"javascript:showContent('".$wr_id."_".$row['wr_id']."');\" width=\"40\" height=\"19\" vspace=\"2\" align=\"absmiddle\" />";
	}
	if($mb_id == $row['mb_id'] || $is_admin == "super" || $is_auth){
		$returnVal .= "\n			&nbsp;<img src=\"../img/product/bt_del.gif\" style=\"cursor:pointer;\" onclick=\"javascript:deleteContent('".$wr_id."', '".$row['wr_id']."', '".$row['step']."', '".$row['family_cnt']."');\" width=\"40\" height=\"19\" vspace=\"2\" align=\"absmiddle\" />";
	}
	
	$returnVal .= "\n		</td>";
	$returnVal .= "\n	</tr>";
	$returnVal .= "\n	<tr id=\"".$wr_id."_".$row['wr_id']."_content\" style=\"display:none\">";
	$returnVal .= "\n		<td>";
	$returnVal .= "\n			<input type=\"text\" name=\"re_content_".$wr_id."_".$row['wr_id']."\" id=\"re_content_".$wr_id."_".$row['wr_id']."\" size=\"110\" />";
	$returnVal .= "\n			<img src=\"../img/product/bt_enter.gif\" style=\"cursor:pointer;\" onclick=\"javascript:writeContent('".$wr_id."', '".$row['wr_id']."', '".$row['orderby']."', '".$row['step']."');\" width=\"46\" height=\"22\" align=\"absmiddle\" />";
	$returnVal .= "\n		</td>";
	$returnVal .= "\n	</tr>";
}
$returnVal .= "</table>";
}

echo $returnVal;

?>